function EntriesAssistant() {
};

EntriesAssistant.prototype.setup = function() {
	Mojo.Log.info("==================================================");
	Mojo.Log.info("Entries Scene");
	Mojo.Log.info("EntriesAssistant.setup()");
	Mojo.Log.info("==================================================");
	
	this.entriesListAttributes = {
		listTemplate: "entries/entries-list-template",
		itemTemplate: "entries/entries-item-template",
		emptyTemplate: "entries/entries-empty-template",
		dividerTemplate: "entries/entries-divider-template",
		dividerFunction: this.entriesDivider,
		hasNoWidgets: true,
		swipeToDelete: true,
		autoConfirmDelete: false
	}
	
	this.entries = [];
	
	this.entriesListModel = {
		items: this.entries
	}
	
	this.controller.setupWidget("entriesList", this.entriesListAttributes, this.entriesListModel);
	
	this.entriesListCommandMenuAttributes = {
		spacerHeight: 0,
		menuClass: "no-fade"
	}
	
	this.entriesListCommandMenuModel = {
		visible: true,
		items: [{
			icon: "new",
			command: "do-edit"
		}]
	}
	
	this.controller.setupWidget(Mojo.Menu.commandMenu, this.entriesListCommandMenuAttributes, this.entriesListCommandMenuModel);
};

EntriesAssistant.prototype.activate = function(event) {

	this.getEntries();
	
	this.showEditHandler = this.showEdit.bindAsEventListener(this);
	this.deleteEntryHandler = this.deleteEntry.bindAsEventListener(this);
	this.controller.listen("entriesList", Mojo.Event.listTap, this.showEditHandler);
	this.controller.listen("entriesList", Mojo.Event.listDelete, this.deleteEntryHandler);
};

EntriesAssistant.prototype.deactivate = function(event) {
	this.controller.stopListening("entriesList", Mojo.Event.listTap, this.showEditHandler);
	this.controller.stopListening("entriesList", Mojo.Event.listDelete, this.deleteEntryHandler);
};

EntriesAssistant.prototype.cleanup = function(event) {
};

EntriesAssistant.prototype.entriesDivider = function(item) {
	return item.description.toString()[0];
};

EntriesAssistant.prototype.showEdit = function(event) {
	this.controller.stageController.pushScene("edit", event.item.entryId);
};

EntriesAssistant.prototype.getEntries = function() {
	if (!OpenHours.dao.db) {
		Mojo.Log.error("Open Hours database is not available");
	} else {
		OpenHours.dao.db.transaction((function(transaction) {
			var sql = "SELECT entryId, description, location, 'OPEN' AS status, 'Status Message Goes Here' AS statusMessage FROM Entries ORDER BY description, location";
			transaction.executeSql(sql, [], this.dbGetEntriesHandler.bind(this), this.dbErrorHandler.bind(this));
		}).bind(this));
	}
};

EntriesAssistant.prototype.dbGetEntriesHandler = function(transaction, results) {
	Mojo.Log.info("==================================================");
	Mojo.Log.info("Entries Scene");
	Mojo.Log.info("EntriesAssistant.getEntriesHandler()");
	Mojo.Log.info("==================================================");
	Mojo.Log.info("Record Count: " + results.rows.length);
	
	this.entries.clear();
	
	if (results.rows.length > 0) {
		for (i = 0; i < results.rows.length; i++) {
			this.entries[i] = {
				entryId: results.rows.item(i).entryId,
				description: results.rows.item(i).description,
				location: results.rows.item(i).location,
				status: results.rows.item(i).status,
				statusMessage: results.rows.item(i).statusMessage
			};
		}
	}
	
	this.controller.setWidgetModel("entriesList", {
		items: this.entries
	});
};

EntriesAssistant.prototype.deleteEntry = function(event) {
	OpenHours.dao.deleteEntry(event.item.entryId);
	
	this.getEntries();
};

EntriesAssistant.prototype.dbErrorHandler = function(transaction, error) {
	Mojo.Log.error("SQL ERROR: " + error.message);
};




